import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _isEmpty from "lodash/isEmpty";
import { queryIdpValue } from 'cpaas-front/lib/services/api';
import { getResponse, createPagination, getCurrentOrganizationId } from 'utils/utils';
import { queryAdminRoles as _queryAdminRoles, queryCreateRolesSubtree, queryInheritRolesTree, queryCode as _queryCode, queryRole as _queryRole, enableRole, queryLabels as _queryLabels, queryPermissionsAll as _queryPermissionsAll, queryOrganizationPermissionsAll, queryRevokedPermissionsAll as _queryRevokedPermissionsAll, queryOrganizationRevokedPermissionsAll, createRole as _createRole, createOrganizationRole, editRole as _editRole, editOrganizationRole, copyRole as _copyRole, copyOrganizationRole, inheritRole as _inheritRole, inheritOrganizationRole, queryLevelPermissions as _queryLevelPermissions, queryOrganizationLevelPermissions, unrevoke as _unrevoke, unrevokeOrganization, queryRoleAuth as _queryRoleAuth, saveRoleAuth as _saveRoleAuth, deleteRoleAuth as _deleteRoleAuth } from "../services/roleService";
var tableState = {
  list: [],
  pagination: {
    pageSize: 10,
    total: 0,
    current: 1
  }
};

function defineProperty(obj, property, value) {
  Object.defineProperty(obj, property, {
    value: value,
    writable: true,
    enumerable: false,
    configurable: true
  });
}

export default {
  namespace: 'role',
  state: {
    code: {},
    adminRoleList: [],
    currentAdminRole: {},
    rolesList: {
      managedRoleList: [],
      awardedRoleList: []
    },
    roleDetail: {
      form: {},
      permissions: _objectSpread({}, tableState),
      revokedPermissions: _objectSpread({}, tableState),
      permissionsDrawer: _objectSpread({}, tableState)
    },
    roleAuth: {},
    roleAuthScopeCode: [],
    roleAuthScopeTypeCode: []
  },
  effects: {
    queryAdminRoles: /*#__PURE__*/_regeneratorRuntime.mark(function queryAdminRoles(action, _ref) {
      var put, call, response;
      return _regeneratorRuntime.wrap(function queryAdminRoles$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              put = _ref.put, call = _ref.call;
              _context.next = 3;
              return call(_queryAdminRoles);

            case 3:
              response = _context.sent;

              if (!(response && !response.failed)) {
                _context.next = 7;
                break;
              }

              _context.next = 7;
              return put({
                type: 'updateStateReducer',
                payload: {
                  adminRoleList: response.map(function (n) {
                    return {
                      value: n.id,
                      title: n.name,
                      assignLevel: n.assignLevel,
                      assignLevelValue: n.assignLevelValue,
                      assignLevelValueMeaning: n.assignLevelValueMeaning,
                      level: n.level
                    };
                  })
                }
              });

            case 7:
            case "end":
              return _context.stop();
          }
        }
      }, queryAdminRoles);
    }),
    queryManagedRoleList: /*#__PURE__*/_regeneratorRuntime.mark(function queryManagedRoleList(_ref2, _ref3) {
      var payload, put, call, response, content, getList, managedRoleList;
      return _regeneratorRuntime.wrap(function queryManagedRoleList$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref2.payload;
              put = _ref3.put, call = _ref3.call;
              _context2.next = 4;
              return call(queryCreateRolesSubtree, payload);

            case 4:
              response = _context2.sent;

              if (!(response && !response.failed)) {
                _context2.next = 11;
                break;
              }

              content = _isEmpty(response) ? [] : [response];

              getList = function getList(collections) {
                var dataSource = collections || content;
                return dataSource.map(function (n) {
                  var m = n;
                  m.key = m.id;

                  if (_isEmpty(m.childRoles)) {
                    return m;
                  } else {
                    m.children = getList(m.childRoles);
                    return m;
                  }
                });
              };

              managedRoleList = getList() || [];
              _context2.next = 11;
              return put({
                type: 'updateStateReducer',
                payload: {
                  rolesList: {
                    managedRoleList: managedRoleList
                  }
                }
              });

            case 11:
            case "end":
              return _context2.stop();
          }
        }
      }, queryManagedRoleList);
    }),
    queryAwardedRoleList: /*#__PURE__*/_regeneratorRuntime.mark(function queryAwardedRoleList(_ref4, _ref5) {
      var payload, put, call, response, setAwardedRoleListChildren, awardedRoleList;
      return _regeneratorRuntime.wrap(function queryAwardedRoleList$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref4.payload;
              put = _ref5.put, call = _ref5.call;
              _context3.next = 4;
              return call(queryInheritRolesTree, payload);

            case 4:
              response = _context3.sent;

              if (!(response && !response.failed)) {
                _context3.next = 10;
                break;
              }

              setAwardedRoleListChildren = function setAwardedRoleListChildren(list) {
                return list.map(function (n) {
                  var item = n;

                  if (!_isEmpty(n.inheritedRole)) {
                    defineProperty(item, 'children', [_objectSpread({}, n.inheritedRole)]);
                  }

                  if (!_isEmpty(item.children)) {
                    item.children = setAwardedRoleListChildren(item.children);
                  }

                  return item;
                });
              };

              awardedRoleList = setAwardedRoleListChildren((response || []).map(function (n) {
                var item = _objectSpread(_objectSpread({
                  key: n.id
                }, n), {}, {
                  inherited: !_isEmpty(n.inheritedRole) ? n.inheritedRole.name : ''
                });

                return item;
              }));
              _context3.next = 10;
              return put({
                type: 'updateStateReducer',
                payload: {
                  rolesList: {
                    awardedRoleList: awardedRoleList
                  }
                }
              });

            case 10:
            case "end":
              return _context3.stop();
          }
        }
      }, queryAwardedRoleList);
    }),
    setRoleEnabled: /*#__PURE__*/_regeneratorRuntime.mark(function setRoleEnabled(_ref6, _ref7) {
      var payload, call, response;
      return _regeneratorRuntime.wrap(function setRoleEnabled$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref6.payload;
              call = _ref7.call;
              _context4.next = 4;
              return call(enableRole, payload);

            case 4:
              response = _context4.sent;
              return _context4.abrupt("return", response && !response.failed);

            case 6:
            case "end":
              return _context4.stop();
          }
        }
      }, setRoleEnabled);
    }),
    queryCode: /*#__PURE__*/_regeneratorRuntime.mark(function queryCode(_ref8, _ref9) {
      var payload, put, call, response;
      return _regeneratorRuntime.wrap(function queryCode$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              payload = _ref8.payload;
              put = _ref9.put, call = _ref9.call;
              _context5.next = 4;
              return call(_queryCode, payload);

            case 4:
              response = _context5.sent;

              if (!(response && !response.failed)) {
                _context5.next = 8;
                break;
              }

              _context5.next = 8;
              return put({
                type: 'setCodeReducer',
                payload: _defineProperty({}, payload.lovCode, response)
              });

            case 8:
            case "end":
              return _context5.stop();
          }
        }
      }, queryCode);
    }),
    queryRole: /*#__PURE__*/_regeneratorRuntime.mark(function queryRole(_ref10, _ref11) {
      var payload, put, call, response;
      return _regeneratorRuntime.wrap(function queryRole$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              payload = _ref10.payload;
              put = _ref11.put, call = _ref11.call;
              _context6.next = 4;
              return call(_queryRole, payload);

            case 4:
              response = _context6.sent;

              if (!(response && !response.failed)) {
                _context6.next = 8;
                break;
              }

              _context6.next = 8;
              return put({
                type: 'updateRoleDetailReducer',
                payload: {
                  form: response
                }
              });

            case 8:
            case "end":
              return _context6.stop();
          }
        }
      }, queryRole);
    }),
    queryLabels: /*#__PURE__*/_regeneratorRuntime.mark(function queryLabels(_ref12, _ref13) {
      var payload, call, response;
      return _regeneratorRuntime.wrap(function queryLabels$(_context7) {
        while (1) {
          switch (_context7.prev = _context7.next) {
            case 0:
              payload = _ref12.payload;
              call = _ref13.call;
              _context7.next = 4;
              return call(_queryLabels, payload);

            case 4:
              response = _context7.sent;
              return _context7.abrupt("return", response && !response.failed ? response : []);

            case 6:
            case "end":
              return _context7.stop();
          }
        }
      }, queryLabels);
    }),
    queryPermissionsAll: /*#__PURE__*/_regeneratorRuntime.mark(function queryPermissionsAll(_ref14, _ref15) {
      var payload, put, call, organizationId, response;
      return _regeneratorRuntime.wrap(function queryPermissionsAll$(_context8) {
        while (1) {
          switch (_context8.prev = _context8.next) {
            case 0:
              payload = _ref14.payload;
              put = _ref15.put, call = _ref15.call;
              organizationId = getCurrentOrganizationId();

              if (!(organizationId !== 0)) {
                _context8.next = 9;
                break;
              }

              _context8.next = 6;
              return call(queryOrganizationPermissionsAll, payload, organizationId);

            case 6:
              response = _context8.sent;
              _context8.next = 12;
              break;

            case 9:
              _context8.next = 11;
              return call(_queryPermissionsAll, payload);

            case 11:
              response = _context8.sent;

            case 12:
              if (!(response && !response.failed)) {
                _context8.next = 15;
                break;
              }

              _context8.next = 15;
              return put({
                type: 'updateRoleDetailReducer',
                payload: {
                  permissions: {
                    list: response || [],
                    pagination: createPagination({
                      number: 0,
                      totalElements: response.length
                    })
                  }
                }
              });

            case 15:
            case "end":
              return _context8.stop();
          }
        }
      }, queryPermissionsAll);
    }),
    queryLevelPermissions: /*#__PURE__*/_regeneratorRuntime.mark(function queryLevelPermissions(_ref16, _ref17) {
      var id, level, _ref16$params, params, call, put, organizationId, res, response, _content, pagination;

      return _regeneratorRuntime.wrap(function queryLevelPermissions$(_context9) {
        while (1) {
          switch (_context9.prev = _context9.next) {
            case 0:
              id = _ref16.id, level = _ref16.level, _ref16$params = _ref16.params, params = _ref16$params === void 0 ? {} : _ref16$params;
              call = _ref17.call, put = _ref17.put;
              organizationId = getCurrentOrganizationId();

              if (!(organizationId !== 0)) {
                _context9.next = 9;
                break;
              }

              _context9.next = 6;
              return call(queryOrganizationLevelPermissions, id, level, params, organizationId);

            case 6:
              res = _context9.sent;
              _context9.next = 12;
              break;

            case 9:
              _context9.next = 11;
              return call(_queryLevelPermissions, id, level, params);

            case 11:
              res = _context9.sent;

            case 12:
              response = getResponse(res);

              if (!(response && !response.failed)) {
                _context9.next = 18;
                break;
              }

              _content = response.content;
              pagination = createPagination(response);
              _context9.next = 18;
              return put({
                type: 'updateRoleDetailPermissionsDrawerReducer',
                payload: {
                  list: _content,
                  pagination: pagination
                }
              });

            case 18:
            case "end":
              return _context9.stop();
          }
        }
      }, queryLevelPermissions);
    }),
    queryRevokedPermissionsAll: /*#__PURE__*/_regeneratorRuntime.mark(function queryRevokedPermissionsAll(_ref18, _ref19) {
      var roleId, payload, put, call, organizationId, response, _response, _content2;

      return _regeneratorRuntime.wrap(function queryRevokedPermissionsAll$(_context10) {
        while (1) {
          switch (_context10.prev = _context10.next) {
            case 0:
              roleId = _ref18.roleId, payload = _ref18.payload;
              put = _ref19.put, call = _ref19.call;
              organizationId = getCurrentOrganizationId();

              if (!(organizationId !== 0)) {
                _context10.next = 9;
                break;
              }

              _context10.next = 6;
              return call(queryOrganizationRevokedPermissionsAll, roleId, payload, organizationId);

            case 6:
              response = _context10.sent;
              _context10.next = 12;
              break;

            case 9:
              _context10.next = 11;
              return call(_queryRevokedPermissionsAll, roleId, payload);

            case 11:
              response = _context10.sent;

            case 12:
              if (!(response && !response.failed)) {
                _context10.next = 16;
                break;
              }

              _response = response, _content2 = _response.content;
              _context10.next = 16;
              return put({
                type: 'updateRoleDetailReducer',
                payload: {
                  revokedPermissions: {
                    list: _content2.map(function (n) {
                      return _objectSpread({
                        key: n.id
                      }, n);
                    }) || [],
                    pagination: createPagination(response)
                  }
                }
              });

            case 16:
            case "end":
              return _context10.stop();
          }
        }
      }, queryRevokedPermissionsAll);
    }),
    createRole: /*#__PURE__*/_regeneratorRuntime.mark(function createRole(_ref20, _ref21) {
      var payload, call, organizationId, response;
      return _regeneratorRuntime.wrap(function createRole$(_context11) {
        while (1) {
          switch (_context11.prev = _context11.next) {
            case 0:
              payload = _ref20.payload;
              call = _ref21.call;
              organizationId = getCurrentOrganizationId();

              if (!(organizationId !== 0)) {
                _context11.next = 9;
                break;
              }

              _context11.next = 6;
              return call(createOrganizationRole, payload, organizationId);

            case 6:
              response = _context11.sent;
              _context11.next = 12;
              break;

            case 9:
              _context11.next = 11;
              return call(_createRole, payload);

            case 11:
              response = _context11.sent;

            case 12:
              return _context11.abrupt("return", getResponse(response));

            case 13:
            case "end":
              return _context11.stop();
          }
        }
      }, createRole);
    }),
    editRole: /*#__PURE__*/_regeneratorRuntime.mark(function editRole(_ref22, _ref23) {
      var payload, call, organizationId, response;
      return _regeneratorRuntime.wrap(function editRole$(_context12) {
        while (1) {
          switch (_context12.prev = _context12.next) {
            case 0:
              payload = _ref22.payload;
              call = _ref23.call;
              organizationId = getCurrentOrganizationId();

              if (!(organizationId !== 0)) {
                _context12.next = 9;
                break;
              }

              _context12.next = 6;
              return call(editOrganizationRole, payload, organizationId);

            case 6:
              response = _context12.sent;
              _context12.next = 12;
              break;

            case 9:
              _context12.next = 11;
              return call(_editRole, payload);

            case 11:
              response = _context12.sent;

            case 12:
              return _context12.abrupt("return", getResponse(response));

            case 13:
            case "end":
              return _context12.stop();
          }
        }
      }, editRole);
    }),
    copyRole: /*#__PURE__*/_regeneratorRuntime.mark(function copyRole(_ref24, _ref25) {
      var payload, call, organizationId, response;
      return _regeneratorRuntime.wrap(function copyRole$(_context13) {
        while (1) {
          switch (_context13.prev = _context13.next) {
            case 0:
              payload = _ref24.payload;
              call = _ref25.call;
              organizationId = getCurrentOrganizationId();

              if (!(organizationId !== 0)) {
                _context13.next = 9;
                break;
              }

              _context13.next = 6;
              return call(copyOrganizationRole, payload, organizationId);

            case 6:
              response = _context13.sent;
              _context13.next = 12;
              break;

            case 9:
              _context13.next = 11;
              return call(_copyRole, payload);

            case 11:
              response = _context13.sent;

            case 12:
              return _context13.abrupt("return", getResponse(response));

            case 13:
            case "end":
              return _context13.stop();
          }
        }
      }, copyRole);
    }),
    inheritRole: /*#__PURE__*/_regeneratorRuntime.mark(function inheritRole(_ref26, _ref27) {
      var payload, call, organizationId, response;
      return _regeneratorRuntime.wrap(function inheritRole$(_context14) {
        while (1) {
          switch (_context14.prev = _context14.next) {
            case 0:
              payload = _ref26.payload;
              call = _ref27.call;
              organizationId = getCurrentOrganizationId();

              if (!(organizationId !== 0)) {
                _context14.next = 9;
                break;
              }

              _context14.next = 6;
              return call(inheritOrganizationRole, payload, organizationId);

            case 6:
              response = _context14.sent;
              _context14.next = 12;
              break;

            case 9:
              _context14.next = 11;
              return call(_inheritRole, payload);

            case 11:
              response = _context14.sent;

            case 12:
              return _context14.abrupt("return", getResponse(response));

            case 13:
            case "end":
              return _context14.stop();
          }
        }
      }, inheritRole);
    }),
    unrevoke: /*#__PURE__*/_regeneratorRuntime.mark(function unrevoke(_ref28, _ref29) {
      var roleId, payload, call, organizationId, response;
      return _regeneratorRuntime.wrap(function unrevoke$(_context15) {
        while (1) {
          switch (_context15.prev = _context15.next) {
            case 0:
              roleId = _ref28.roleId, payload = _ref28.payload;
              call = _ref29.call;
              organizationId = getCurrentOrganizationId();

              if (!(organizationId !== 0)) {
                _context15.next = 9;
                break;
              }

              _context15.next = 6;
              return call(unrevokeOrganization, roleId, payload, organizationId);

            case 6:
              response = _context15.sent;
              _context15.next = 12;
              break;

            case 9:
              _context15.next = 11;
              return call(_unrevoke, roleId, payload);

            case 11:
              response = _context15.sent;

            case 12:
              return _context15.abrupt("return", response && !response.failed);

            case 13:
            case "end":
              return _context15.stop();
          }
        }
      }, unrevoke);
    }),
    queryRoleAuthScopeCode: /*#__PURE__*/_regeneratorRuntime.mark(function queryRoleAuthScopeCode(_, _ref30) {
      var call, put, res, roleAuthScopeCode;
      return _regeneratorRuntime.wrap(function queryRoleAuthScopeCode$(_context16) {
        while (1) {
          switch (_context16.prev = _context16.next) {
            case 0:
              call = _ref30.call, put = _ref30.put;
              _context16.next = 3;
              return call(queryIdpValue, 'CIAM.AUTHORITY_SCOPE_CODE');

            case 3:
              res = _context16.sent;
              roleAuthScopeCode = getResponse(res);
              _context16.next = 7;
              return put({
                type: 'updateStateReducer',
                payload: {
                  roleAuthScopeCode: roleAuthScopeCode
                }
              });

            case 7:
              return _context16.abrupt("return", roleAuthScopeCode);

            case 8:
            case "end":
              return _context16.stop();
          }
        }
      }, queryRoleAuthScopeCode);
    }),
    queryRoleAuthScopeTypeCode: /*#__PURE__*/_regeneratorRuntime.mark(function queryRoleAuthScopeTypeCode(_, _ref31) {
      var call, put, res, roleAuthScopeTypeCode;
      return _regeneratorRuntime.wrap(function queryRoleAuthScopeTypeCode$(_context17) {
        while (1) {
          switch (_context17.prev = _context17.next) {
            case 0:
              call = _ref31.call, put = _ref31.put;
              _context17.next = 3;
              return call(queryIdpValue, 'CIAM.AUTHORITY_TYPE_CODE');

            case 3:
              res = _context17.sent;
              roleAuthScopeTypeCode = getResponse(res);
              _context17.next = 7;
              return put({
                type: 'updateStateReducer',
                payload: {
                  roleAuthScopeTypeCode: roleAuthScopeTypeCode
                }
              });

            case 7:
              return _context17.abrupt("return", roleAuthScopeTypeCode);

            case 8:
            case "end":
              return _context17.stop();
          }
        }
      }, queryRoleAuthScopeTypeCode);
    }),
    queryRoleAuth: /*#__PURE__*/_regeneratorRuntime.mark(function queryRoleAuth(_ref32, _ref33) {
      var payload, call, put, res, roleAuth;
      return _regeneratorRuntime.wrap(function queryRoleAuth$(_context18) {
        while (1) {
          switch (_context18.prev = _context18.next) {
            case 0:
              payload = _ref32.payload;
              call = _ref33.call, put = _ref33.put;
              _context18.next = 4;
              return call(_queryRoleAuth, payload);

            case 4:
              res = _context18.sent;
              roleAuth = getResponse(res);
              _context18.next = 8;
              return put({
                type: 'updateStateReducer',
                payload: {
                  roleAuth: roleAuth
                }
              });

            case 8:
              return _context18.abrupt("return", roleAuth);

            case 9:
            case "end":
              return _context18.stop();
          }
        }
      }, queryRoleAuth);
    }),
    saveRoleAuth: /*#__PURE__*/_regeneratorRuntime.mark(function saveRoleAuth(_ref34, _ref35) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function saveRoleAuth$(_context19) {
        while (1) {
          switch (_context19.prev = _context19.next) {
            case 0:
              payload = _ref34.payload;
              call = _ref35.call;
              _context19.next = 4;
              return call(_saveRoleAuth, payload);

            case 4:
              res = _context19.sent;
              return _context19.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context19.stop();
          }
        }
      }, saveRoleAuth);
    }),
    deleteRoleAuth: /*#__PURE__*/_regeneratorRuntime.mark(function deleteRoleAuth(_ref36, _ref37) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function deleteRoleAuth$(_context20) {
        while (1) {
          switch (_context20.prev = _context20.next) {
            case 0:
              payload = _ref36.payload;
              call = _ref37.call;
              _context20.next = 4;
              return call(_deleteRoleAuth, payload);

            case 4:
              res = _context20.sent;
              return _context20.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context20.stop();
          }
        }
      }, deleteRoleAuth);
    })
  },
  reducers: {
    updateStateReducer: function updateStateReducer(state, _ref38) {
      var payload = _ref38.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    },
    setCodeReducer: function setCodeReducer(state, _ref39) {
      var payload = _ref39.payload;
      return _objectSpread(_objectSpread({}, state), {}, {
        code: Object.assign(state.code, payload)
      });
    },
    updateRoleDetailReducer: function updateRoleDetailReducer(state, _ref40) {
      var payload = _ref40.payload;
      return _objectSpread(_objectSpread({}, state), {}, {
        roleDetail: _objectSpread(_objectSpread({}, state.roleDetail), payload)
      });
    },
    initRoleDetailReducer: function initRoleDetailReducer(state) {
      return _objectSpread(_objectSpread({}, state), {}, {
        roleDetail: Object.assign(state.roleDetail, {
          form: {},
          permissions: _objectSpread({}, tableState),
          revokedPermissions: _objectSpread({}, tableState)
        })
      });
    },
    updateRoleDetailPermissionsDrawerReducer: function updateRoleDetailPermissionsDrawerReducer(state, _ref41) {
      var payload = _ref41.payload;
      return _objectSpread(_objectSpread({}, state), {}, {
        roleDetail: _objectSpread(_objectSpread({}, state.roleDetail), {}, {
          permissionsDrawer: _objectSpread(_objectSpread({}, state.roleDetail.permissionsDrawer), payload)
        })
      });
    }
  }
};